﻿using Tetris.Game.Common.TetrisCenter;
using Tetris.Game.Common.TetrisCenter.TetrisBase;
using Tetris.Game.ControlsBase;
using Tetris.Game.Properties;

namespace Tetris.Game
{
    public partial class FrmRight : Form
    {
        private Color _color = Color.FromArgb(30, 217, 236, 255);
        private readonly TetrisGameCenter _tetrisGameCenter;
        public FrmRight(TetrisGameCenter tetrisGame)
        {
            InitializeComponent();
            _tetrisGameCenter = tetrisGame;
            SetStyle(
                 ControlStyles.ResizeRedraw
                 | ControlStyles.DoubleBuffer
                 | ControlStyles.UserPaint
                 | ControlStyles.AllPaintingInWmPaint
                 | ControlStyles.SupportsTransparentBackColor,
                 true
                 );
            this.UpdateStyles();
            this.DoubleBuffered = true;
            BackColor = Color.Transparent;
            sspMenu.Cursor = Cursors.Hand;
        }
        public void GameResult(TetrisGameResult gameResult)
        {
            this.ExecBeginInvoke(() =>
            {
                lblLevel.Text = gameResult.Level.ToString();
                lblRow.Text = gameResult.RowNumber.ToString();
                lblScore.Text = gameResult.Score.ToString();
            });
        }
        public void InitData()
        {
            this.ExecBeginInvoke(() =>
            {
                lblLevel.Text = 1.ToString();
                lblRow.Text = 0.ToString();
                lblScore.Text = 0.ToString();
            });
        }
        private TetrisShapeBase _lastTetrisShape;
        public void NextTetrisShape(TetrisShapeBase tsb)
        {
            this.ExecBeginInvoke(() =>
            {
                if (tsb is null)
                {
                    this.Controls.Remove(_lastTetrisShape);
                    _lastTetrisShape.DisposeEx();
                    return;
                }

                tsb.Location = new Point((this.Width - tsb.Width) >> 1, 15);

                this.Controls.Remove(_lastTetrisShape);
                _lastTetrisShape.DisposeEx();
                this.Controls.Add(tsb);
                _lastTetrisShape = tsb;
            });
        }
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);

            using (var brush = new SolidBrush(_color))
            {
                e.Graphics.FillRectangle(brush, ClientRectangle);
            }
        }

        private void pbStartGame_Click(object sender, EventArgs e)
        {
            if (_tetrisGameCenter._gameCneterEvent != null) return;
            if (_tetrisGameCenter.GameStatus == Common.TetrisCenter.TetrisBase.GameStatus.Start)
            {
                pbStartGame.Image = Resources.Start;
                _tetrisGameCenter.GameStatus = Common.TetrisCenter.TetrisBase.GameStatus.Suspend;
            }
            else
            {
                pbStartGame.Image = Resources.suspend2;
                _tetrisGameCenter.GameStatus = Common.TetrisCenter.TetrisBase.GameStatus.Start;
            }

        }
        public void StopGame()
        {
            pbStartGame.Image = Resources.Start;
            MessageBox.Show("游戏已结束~~~");
        }


        private void sspMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            switch (e.ClickedItem.Name)
            {
                case "tsslMonitor":

                    break;
            }
        }
    }
}
